---
title: Clearing & Resetting
layout: page
pageOrder: 4
section: 'General'
subsection: true
sitemap:
  priority: 0.7
  changefreq: 'monthly'
  lastmod: 2019-11-10T08:00:00+01:00
---

<p>MockServer has the following internal state:</p>

<ul>
    <li><strong>recorded requests</strong></li>
    <li><strong>active expectations</strong></li>
    <li><strong>recorded expectations</strong></li>
    <li><strong>logs</strong></li>
</ul>

<p>State can be <strong>cleared</strong> from MockServer selectively:</p>
<ul>
    <li>use <strong>type</strong> to select which type of state to clear, supported values are: <span class="string_literal">all</span>, <span class="string_literal">log</span>, <span class="string_literal">expectations</span></li>
    <li>use a <a href="/mock_server/getting_started.html#request_matchers"><strong>request matcher</strong></a> to clear matching items</li>
    <li>use an <strong>expectation id</strong> to clear matching items</li>
</ul>

<p>When <a href="/mock_server/debugging_issues.html#logging-levels">logLevel</a> is set to <span class="string_literal">DEBUG</span> using <strong>clear</strong> does not delete logs from the event log, instead they are marked as deleted to ensure <a href="/mock_server/verification.html">verification</a> continues to work correctly.  This allows the <a href="/mock_server/mockserver_ui.html">UI</a> to continue to display logs that would have otherwise been deleted to improve the ability to debug issues.</p>

<button id="button_client_clear_all_logs" class="accordion">clear all recorded requests and logs</button>
<div class="panel">
    <button class="accordion inner">Java: MockServerClient</button>
    <div class="panel">
            <pre class="prettyprint lang-java code"><code class="code">new MockServerClient("localhost", 1080).clear(
    request(),
    ClearType.LOG
);</code></pre>
    </div>
    <button class="accordion inner">JavaScript: mockserver-client</button>
    <div class="panel">
        <pre class="prettyprint lang-javascript code"><code class="code">var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
  .clear({}, 'LOG')
  .then(
    function () {
      console.log("cleared recorded requests and logs that matches request matcher");
    },
    function (error) {
      console.log(error);
    }
  );</code></pre>
        <p>See <a target="_blank" href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_clear" target="_blank">REST API</a> for full JSON specification</p>
    </div>
    <button class="accordion inner">REST API</button>
    <div class="panel">
        <pre class="prettyprint code"><code class="code">curl -v -X PUT "http://localhost:1080/mockserver/clear?type=log"</code></pre>
        <p>See <a target="_blank" href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_clear" target="_blank">REST API</a> for full JSON specification</p>
    </div>
</div>
<button id="button_client_clear" class="accordion">clear everything that matches request properties matcher</button>
<div class="panel">
    <button class="accordion inner">Java: MockServerClient</button>
    <div class="panel">
            <pre class="prettyprint lang-java code"><code class="code">new MockServerClient("localhost", 1080).clear(
    request()
        .withPath("/some/path")
);</code></pre>
    </div>
    <button class="accordion inner">JavaScript: mockserver-client</button>
    <div class="panel">
        <pre class="prettyprint lang-javascript code"><code class="code">var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
  .clear({
    'path': '/some/path'
  })
  .then(
    function () {
      console.log("cleared state that matches request matcher");
    },
    function (error) {
      console.log(error);
    }
  );</code></pre>
        <p>See <a target="_blank" href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_clear" target="_blank">REST API</a> for full JSON specification</p>
    </div>
    <button class="accordion inner">REST API</button>
    <div class="panel">
        <pre class="prettyprint code"><code class="code">curl -v -X PUT "http://localhost:1080/mockserver/clear" -d '{
    "path": "/some/path"
}'</code></pre>
        <p>See <a target="_blank" href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_clear" target="_blank">REST API</a> for full JSON specification</p>
    </div>
</div>
<button id="button_client_openapi_clear" class="accordion">clear everything that matches openapi request matcher</button>
<div class="panel">
    <button class="accordion inner">Java: MockServerClient</button>
    <div class="panel">
            <pre class="prettyprint lang-java code"><code class="code">new MockServerClient("localhost", 1080).clear(
    openAPI(
        "https://raw.githubusercontent.com/mock-server/mockserver/master/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json",
        "showPetById"
    )
);</code></pre>
    </div>
    <button class="accordion inner">JavaScript: mockserver-client</button>
    <div class="panel">
        <pre class="prettyprint lang-javascript code"><code class="code">var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
    .clear({
        "specUrlOrPayload": "https://raw.githubusercontent.com/mock-server/mockserver/master/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json",
        "operationId": "showPetById"
    })
    .then(
        function () {
            console.log("cleared state that matches request matcher");
        },
        function (error) {
            console.log(error);
        }
    );</code></pre>
        <p>See <a target="_blank" href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_clear" target="_blank">REST API</a> for full JSON specification</p>
    </div>
    <button class="accordion inner">REST API</button>
    <div class="panel">
        <pre class="prettyprint code"><code class="code">curl -v -X PUT "http://localhost:1080/mockserver/clear" -d '{
        "specUrlOrPayload": "https://raw.githubusercontent.com/mock-server/mockserver/master/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json",
        "operationId": "showPetById"
    }'</code></pre>
        <p>See <a target="_blank" href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_clear" target="_blank">REST API</a> for full JSON specification</p>
    </div>
</div>
<button id="button_client_expectation_id_clear" class="accordion">clear everything that matches expectation id</button>
<div class="panel">
    <button class="accordion inner">Java: MockServerClient</button>
    <div class="panel">
            <pre class="prettyprint lang-java code"><code class="code">new MockServerClient("localhost", 1080)
    .clear("31e4ca35-66c6-4645-afeb-6e66c4ca0559");</code></pre>
    </div>
    <button class="accordion inner">JavaScript: mockserver-client</button>
    <div class="panel">
        <pre class="prettyprint lang-javascript code"><code class="code">var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
    .clearById("31e4ca35-66c6-4645-afeb-6e66c4ca0559")
    .then(
        function () {
            console.log("cleared state that matches expectation id");
        },
        function (error) {
            console.log(error);
        }
    );</code></pre>
        <p>See <a target="_blank" href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_clear" target="_blank">REST API</a> for full JSON specification</p>
    </div>
    <button class="accordion inner">REST API</button>
    <div class="panel">
        <pre class="prettyprint code"><code class="code">curl -X PUT "http://localhost:1080/mockserver/clear" -d '{
    "id": "31e4ca35-66c6-4645-afeb-6e66c4ca0559"
}'</code></pre>
        <p>See <a target="_blank" href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_clear" target="_blank">REST API</a> for full JSON specification</p>
    </div>
</div>
<button id="button_client_clear_logs_by_req" class="accordion">clear recorded requests and logs that matches request properties matcher</button>
<div class="panel">
    <button class="accordion inner">Java: MockServerClient</button>
    <div class="panel">
            <pre class="prettyprint lang-java code"><code class="code">new MockServerClient("localhost", 1080).clear(
    request()
        .withPath("/some/path"),
    ClearType.LOG
);</code></pre>
    </div>
    <button class="accordion inner">JavaScript: mockserver-client</button>
    <div class="panel">
        <pre class="prettyprint lang-javascript code"><code class="code">var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
  .clear({
    'path': '/some/path'
  }, 'LOG')
  .then(
    function () {
      console.log("cleared recorded requests and logs that matches request matcher");
    },
    function (error) {
      console.log(error);
    }
  );</code></pre>
        <p>See <a target="_blank" href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_clear" target="_blank">REST API</a> for full JSON specification</p>
    </div>
    <button class="accordion inner">REST API</button>
    <div class="panel">
        <pre class="prettyprint code"><code class="code">curl -v -X PUT "http://localhost:1080/mockserver/clear?type=log" -d '{
    "path": "/some/path"
}'</code></pre>
        <p>See <a target="_blank" href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_clear" target="_blank">REST API</a> for full JSON specification</p>
    </div>
</div>
<button id="button_client_clear_logs_by_openapi_req" class="accordion">clear recorded requests and logs that matches openapi request matcher</button>
<div class="panel">
    <button class="accordion inner">Java: MockServerClient</button>
    <div class="panel">
            <pre class="prettyprint lang-java code"><code class="code">new MockServerClient("localhost", 1080).clear(
    openAPI(
        "https://raw.githubusercontent.com/mock-server/mockserver/master/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json",
        "showPetById"
    ),
    ClearType.LOG
);</code></pre>
    </div>
    <button class="accordion inner">JavaScript: mockserver-client</button>
    <div class="panel">
        <pre class="prettyprint lang-javascript code"><code class="code">var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
    .clear({
        "specUrlOrPayload": "https://raw.githubusercontent.com/mock-server/mockserver/master/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json",
        "operationId": "showPetById"
    }, 'LOG')
    .then(
        function () {
            console.log("cleared state that matches request matcher");
        },
        function (error) {
            console.log(error);
        }
    );</code></pre>
        <p>See <a target="_blank" href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_clear" target="_blank">REST API</a> for full JSON specification</p>
    </div>
    <button class="accordion inner">REST API</button>
    <div class="panel">
        <pre class="prettyprint code"><code class="code">curl -v -X PUT "http://localhost:1080/mockserver/clear?type=log" -d '{
        "specUrlOrPayload": "https://raw.githubusercontent.com/mock-server/mockserver/master/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json",
        "operationId": "showPetById"
    }'</code></pre>
        <p>See <a target="_blank" href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_clear" target="_blank">REST API</a> for full JSON specification</p>
    </div>
</div>
<button id="button_client_clear_expectations_by_req" class="accordion">clear expectations that matches request matcher</button>
<div class="panel">
    <button class="accordion inner">Java: MockServerClient</button>
    <div class="panel">
            <pre class="prettyprint lang-java code"><code class="code">new MockServerClient("localhost", 1080).clear(
    request()
        .withPath("/some/path"),
    ClearType.EXPECTATIONS
);</code></pre>
    </div>
    <button class="accordion inner">JavaScript: mockserver-client</button>
    <div class="panel">
        <pre class="prettyprint lang-javascript code"><code class="code">var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
  .clear({
    'path': '/some/path'
  }, 'EXPECTATIONS')
  .then(
    function () {
      console.log("cleared expectations that matches request matcher");
    },
    function (error) {
      console.log(error);
    }
  );</code></pre>
        <p>See <a target="_blank" href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_clear" target="_blank">REST API</a> for full JSON specification</p>
    </div>
    <button class="accordion inner">REST API</button>
    <div class="panel">
        <pre class="prettyprint code"><code class="code">curl -v -X PUT "http://localhost:1080/mockserver/clear?type=expectations" -d '{
    "path": "/some/path"
}'</code></pre>
        <p>See <a target="_blank" href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_clear" target="_blank">REST API</a> for full JSON specification</p>
    </div>
</div>

<p>MockServer can be <strong>reset</strong> completely, as follows:</p>

<button id="button_client_reset" class="accordion">reset everything</button>
<div class="panel">
    <button class="accordion inner">Java: MockServerClient</button>
    <div class="panel">
        <pre class="prettyprint lang-java code"><code class="code">new MockServerClient("localhost", 1080).reset();</code></pre>
    </div>
    <button class="accordion inner">JavaScript: mockserver-client</button>
    <div class="panel">
        <pre class="prettyprint lang-javascript code"><code class="code">var mockServerClient = require('mockserver-client').mockServerClient;
mockServerClient("localhost", 1080)
  .reset()
  .then(
    function () {
      console.log("reset all state");
    },
    function (error) {
      console.log(error);
    }
  );</code></pre>
        <p>See <a target="_blank" href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_reset" target="_blank">REST API</a> for full JSON specification</p>
    </div>
    <button class="accordion inner">REST API</button>
    <div class="panel">
        <pre class="prettyprint code"><code class="code">curl -v -X PUT "http://localhost:1080/mockserver/reset</code></pre>
        <p>See <a target="_blank" href="https://app.swaggerhub.com/apis/jamesdbloom/mock-server-openapi/{{ site.mockserver_api_version }}#/control/put_reset" target="_blank">REST API</a> for full JSON specification</p>
    </div>
</div>
